
package com.huatec.hiot_cloud.core.config;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.paths.SwaggerPathProvider;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

 // SwaggerUI配置


//用@Configuration注解该类，等价于XML中配置beans
//如果需要使用junit进行测试，需注释掉@Configuration


@Configuration
//@EnableSwagger：使swagger生效
@EnableSwagger
//@EnableWebMvc 启动Spring MVC支持
@EnableWebMvc
//@ComponentScan 配置包扫描
//@ComponentScan(basePackages ={"com.huatec.hiot_cloud.core"})
public class SwaggerConfig extends WebMvcConfigurerAdapter {

	private SpringSwaggerConfig springSwaggerConfig;
	//需要自动注入SpringSwaggerConfig
	@Autowired
	public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig){
		this.springSwaggerConfig = springSwaggerConfig;
	}
	//用@Bean标注方法等价于XML中配置bean
	@Bean
	public SwaggerSpringMvcPlugin customImplementation(){
		return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
				.apiInfo(apiInfo())
				.includePatterns(".*?")//路径扫描
				.swaggerGroup("hiot")

				//.resourceGroupingStrategy()
				;
				//.pathProvider()
				//.pathProvider(new GtPaths())
	}
	private ApiInfo apiInfo(){
		ApiInfo apiInfo = new ApiInfo(
				"华晟物联云API",
				"云平台Open API",    //My Apps API Description",
				"http://www.huatec.com",//My Apps API terms of service
				"zhang.jing@huatec.com",//My Apps API Contact Email
				"华晟经世", //"My Apps API Licence Type"
				"http://www.huatec.com");//My Apps API License URL
		return apiInfo;
	}

	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	class GtPaths extends SwaggerPathProvider {

		@Override
		protected String applicationPath() {
			return "/restapi";
		}

		@Override
		protected String getDocumentationPath() {
			return "/restapi";
		}
	}

}


